# DBSCAN 实例，sklearn
import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import DBSCAN
from sklearn.datasets import make_blobs

np.random.seed(123)
X, y = make_blobs(centers=4, n_samples=1000)#生成4个随机簇
print('数据的维数:', X.shape)
print('数据的簇标签：', y)

plt.figure()
plt.scatter(X[:,0], X[:,1], c=y)
plt.title("Dataset with 4 clusters")
plt.xlabel("X")
plt.ylabel("Y")

eps = 2
min_samples = 3

model = DBSCAN(eps = eps, min_samples = min_samples)  #
model.fit(X)
label_pred = model.labels_

print('预测标签', label_pred)
components = model.components_  ##核心对象

print('核心对象坐标：', components)

plt.figure()
plt.scatter(X[:,0], X[:,1], c=label_pred)
plt.title('eps=%.1f, min_samples=%d' % (eps, min_samples))
plt.show()